package com.dm.cloud.file.controller;

import com.dm.cloud.core.mode.ResultModel;
import com.dm.cloud.file.common.model.file.*;
import com.dm.cloud.file.service.FIleManagerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/apis/v1/fileManager")
@Tag(name =  "文件管理服务")
public class FIleManagerController {

    @Autowired
    private FIleManagerService fIleManagerService;

    @GetMapping("/files")
    @Operation(summary = "查询文件路径下的文件列表")
    public ResultModel<List<FileModel>> files(
            @Parameter(description = "父路径(只能是目录),根路径使用 /",in = ParameterIn.QUERY,required = true) String parent,
            @Parameter(description = "当前页文件名查询条件",in = ParameterIn.QUERY) String filterName,
            @Parameter(description = "文件类型查询条件,为空则查询全部",in = ParameterIn.QUERY) FileModel.FileType fileType
            ){
        List<FileModel> files=fIleManagerService.files(parent,filterName,fileType);
        return  ResultModel.success(files);
    }

    @PostMapping("upFileStart")
    @Operation(summary = "分片上传文件第一步：获取上传文件标识")
    public ResultModel<FileUploadBurstStartResultMode> upImageStart(@RequestBody FileUploadBurstStartMode fileUploadShardStartMode){
        FileUploadBurstStartResultMode re=fIleManagerService.shardStart(fileUploadShardStartMode);
        return ResultModel.success(re);
    }

    @PostMapping("filehardUpload")
    @Operation(summary = "上传分片文件第二步：上传分片。 fileKey:第一个步骤返回的key, shardNum:当前分片编号 ")
    public ResultModel<Boolean> upImageShardUpload(MultipartFile file, String fileKey, Integer shardNum){
        Boolean isOk= fIleManagerService.shardUpload(file,fileKey,shardNum);
        return ResultModel.success(isOk);
    }

    @PostMapping("fileShardMerge")
    @Operation(summary = "上传分片文件第三步：合并分片。并上传镜像，返回相对路径")
    public ResultModel<FileModel> shardMerge(@RequestBody FileUploadMerge fileUploadMerge){
        FileModel fileModel= fIleManagerService.shardMerge(fileUploadMerge,0);
        return ResultModel.success(fileModel);
    }

    @PostMapping("deleteFile")
    @Operation(summary = "删除文件/目录")
    public ResultModel<Boolean> deleteFile(@RequestBody DeleteFileModel deleteFileModel){
        Boolean isDelete= fIleManagerService.deleteFile(deleteFileModel);
        return ResultModel.success(isDelete);
    }

    @PostMapping("updateName")
    @Operation(summary = "修改文件名")
    public ResultModel<Boolean> updateName(@RequestBody UpdateFileNameModel updateFileNameModel){
        Boolean isDelete= fIleManagerService.updateName(updateFileNameModel);
        return ResultModel.success(isDelete);
    }

    @PostMapping("mkdirs")
    @Operation(summary = "创建空文件夹")
    public ResultModel<FileModel> mkdirs(@Parameter(description = "要创建的目录全路径/",in = ParameterIn.QUERY,required = true) String path){
        path=path.replace("\\","/");
        if(!path.startsWith("/")){
            path="/"+path;
        }
        FileModel fileModel= fIleManagerService.mkdirs(path);
        return ResultModel.success(fileModel);
    }


    @GetMapping("/download")
    @Operation(summary = "下载文件")
    public void downloadFile(
            @Parameter(description = "文件路径 / 开头",in = ParameterIn.QUERY,required = true) String filePath,
            HttpServletResponse response
    ){
        filePath=filePath.replace("\\","/");
        if(!filePath.startsWith("/")){
            filePath="/"+filePath;
        }
//        try {
//            response.sendRedirect("/equip-file-service/static/"+filePath);
//        } catch (IOException e) {
//            throw new CustomException("下载失败!");
//        }
        fIleManagerService.downloadFile(filePath,response);
    }

    @PostMapping("/downloadFiles")
    @Operation(summary = "批量下载文件")
    public void downloadFiles(
            @RequestBody List<String> filePaths,
            HttpServletResponse response
    ){
        fIleManagerService.downloadFiles(filePaths,response);
    }

}
